//
//  SettingTurnNameViewController.m
//  SIqICalendar
//
//  Created by sunjiawen on 17/3/8.
//  Copyright © 2017年 sjw. All rights reserved.
//

#import "SettingTurnNameViewController.h"
#import "SettingTurnPeriodViewController.h"
#import "JWCalendarModel.h"

@interface SettingTurnNameViewController ()<UITextFieldDelegate>

@end

@implementation SettingTurnNameViewController
{
    UILabel     *_titleLabel;
    UITextField *_turnNameTextField;
    NSString    *_turnName;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initalTitleView];
    
    [self initialData];
    
    [self createView];
}

- (void)initialData {
    if (self.calendarModel) {
        _turnName = self.calendarModel.calendarName;
    }else {
        _turnName = @"";
    }
}

- (void)createView {
    
    UIView  *lineV = [[UIView alloc]init];
    lineV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lineV];
    
    CGFloat leftMargin = WGiveWidth(30.f);
    CGFloat topMargin = WGiveHeight(150.f);
    CGFloat lineVWidth = kScreenWidth - 2*leftMargin;
    [lineV makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftMargin);
        make.width.equalTo(lineVWidth);
        make.height.equalTo(2);
        make.top.equalTo(self.view.top).equalTo(topMargin);
    }];
    
    // 说明label
    UILabel *notesLabelInTop = [[UILabel alloc]init];
    notesLabelInTop.text = @"给这个排班表起一个霸气的名字吧~";
    notesLabelInTop.textColor = UIColorFromRGB(0xCCCCCC);
    notesLabelInTop.font = [UIFont systemFontOfSize:defaultFont];
    notesLabelInTop.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:notesLabelInTop];
    
    [notesLabelInTop makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.top.equalTo(lineV.bottom).offset(WGiveHeight(10));
    }];
    
    // 倒班名称输入框
    _turnNameTextField = [[UITextField alloc]initWithFrame:CGRectZero];
    _turnNameTextField.textAlignment = NSTextAlignmentCenter;
    _turnNameTextField.font = [UIFont boldSystemFontOfSize:25.f];
    [_turnNameTextField setAdjustsFontSizeToFitWidth:YES];
    _turnNameTextField.backgroundColor = [UIColor clearColor];
    _turnNameTextField.textColor = [UIColor whiteColor];
    _turnNameTextField.text = _turnName;
    _turnNameTextField.delegate = self;
    
    NSMutableAttributedString *attributePlaceholder = [[NSMutableAttributedString alloc]initWithString:@"点这里输入排班表的名称" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithWhite:0.8 alpha:1],NSFontAttributeName : [UIFont systemFontOfSize:25.f]}];
    _turnNameTextField.attributedPlaceholder = attributePlaceholder;
    
    [_turnNameTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_turnNameTextField];
    
    [_turnNameTextField makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.bottom.equalTo(lineV.top).offset(WGiveHeight(-8));
    }];
    
    // 下一步 按钮
    UIButton *nextStepBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextStepBtn setBackgroundColor:[UIColor clearColor]];
    nextStepBtn.titleLabel.font = [UIFont boldSystemFontOfSize:25.f];
    [nextStepBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextStepBtn.layer.masksToBounds = YES;
    nextStepBtn.layer.cornerRadius = 10;
    nextStepBtn.layer.borderWidth = 2;
    nextStepBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    [nextStepBtn addTarget:self action:@selector(nextStepAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: nextStepBtn];
    
    [nextStepBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(notesLabelInTop.bottom).offset(100);
        make.width.equalTo(WGiveWidth(150.f));
        make.height.equalTo(WGiveHeight(50.f));
        make.centerX.equalTo(self.view.centerX);
    }];
    
    _turnNameTextField = [[UITextField alloc]init];
    
    [self.view addSubview:_turnNameTextField];
    
}

- (void)initalTitleView {
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont systemFontOfSize:CalendarTitleFont];
    _titleLabel.textColor = TitleColor;
    _titleLabel.text = self.isEdit ? @"编辑排班（1/3）" : @"创建排班（1/3）";
    
    self.navigationItem.titleView = _titleLabel;
    
}

/**
 下一步
 */
- (void)nextStepAction:(UIButton *)btn {
    [self.view endEditing:YES];
    
    if ([self isBlankString:_turnName]) {
        [@"先给我起个名吧~😄" show_alert_title:@"大侠请留步~"];
        return;
    }
    
    SettingTurnPeriodViewController *vc = [[SettingTurnPeriodViewController alloc]initWithNibName:NSStringFromClass([SettingTurnPeriodViewController class]) bundle:nil];
    vc.turnName = _turnName;
    vc.isEdit = self.isEdit;
    if (self.calendarModel) {
        vc.calendarModel = self.calendarModel;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)textFieldDidChange:(UITextField *)textField
{
    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 11) {
                [@"差不多行了~\n11个字还不够啊" show_alert_title:nil];
                textField.text = [toBeString substringToIndex:11];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > 11) {
            [@"差不多行了~\n11个字还不够啊" show_alert_title:nil];
            textField.text = [toBeString substringToIndex:11];
        }
    }
    
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    _turnName = textField.text;
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
